﻿using B.Z.BasicData.API.Read.DTO.ExcuseDtos;
using B.Z.BasicData.Domain.Entity.Excused;
using B.Z.BasicData.ErrorCode;
using B.Z.BasicData.Infrastructure;
using MediatR;

namespace B.Z.BasicData.API.Read.Application.Excused
{
    public class ExcuseFormQueryCommandHanler : IRequestHandler<ExcuseFormQueryCommand, APIResult<APIPaging<ExcusedFormDtos>>>
    {
        private readonly IBaseRepository<T_Excused> excuseRepository;

        public ExcuseFormQueryCommandHanler(IBaseRepository<T_Excused> excuseRepository)
        {
            this.excuseRepository = excuseRepository;
        }

        public Task<APIResult<APIPaging<ExcusedFormDtos>>> Handle(ExcuseFormQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<ExcusedFormDtos>> result = new APIResult<APIPaging<ExcusedFormDtos>>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            var list = from a in excuseRepository.GetAll().Where(x => x.Status == 1 && x.Remark == 1)
                       group a by a.Name into Form
                       select new ExcusedFormDtos
                       {
                           UserName = Form.Key,
                           Jan = Form.Where(x => x.StartDateTime.Month == 1 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Feb = Form.Where(x => x.StartDateTime.Month == 2 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Mar = Form.Where(x => x.StartDateTime.Month == 3 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Apr = Form.Where(x => x.StartDateTime.Month == 4 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           May = Form.Where(x => x.StartDateTime.Month == 5 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Jun = Form.Where(x => x.StartDateTime.Month == 6 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Jul = Form.Where(x => x.StartDateTime.Month == 7 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Aug = Form.Where(x => x.StartDateTime.Month == 8 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Sept = Form.Where(x => x.StartDateTime.Month == 9 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Oct = Form.Where(x => x.StartDateTime.Month == 10 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Nov = Form.Where(x => x.StartDateTime.Month == 11 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                           Dec = Form.Where(x => x.StartDateTime.Month == 12 && x.EndDateTime.Year == DateTime.Now.Year).Sum(x => x.TotalHour*8),
                       };
            if (!string.IsNullOrEmpty(request.UserName)) 
            {
                list = list.Where(x => x.UserName.Contains(request.UserName));
            }
            APIPaging<ExcusedFormDtos> paging = new APIPaging<ExcusedFormDtos>();
            paging.RecordCount = list.Count();
            paging.List = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = paging;
            return Task.FromResult(result);
        }
    }
}
